package com.stylefeng.guns.modular.purchase.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.PurchaseGoods;
import com.stylefeng.guns.modular.purchase.service.IPurchaseGoodsService;

/**
 * 进货单据查询控制器
 *
 * @author fengshuonan
 * @Date 2018-09-12 21:06:48
 */
@Controller
@RequestMapping("/purchaseGoods")
public class PurchaseGoodsController extends BaseController {

    private String PREFIX = "/purchase/purchaseGoods/";

    @Autowired
    private IPurchaseGoodsService purchaseGoodsService;

    /**
     * 跳转到进货单据查询首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "purchaseGoods.html";
    }

    /**
     * 跳转到添加进货单据查询
     */
    @RequestMapping("/purchaseGoods_add")
    public String purchaseGoodsAdd() {
        return PREFIX + "purchaseGoods_detail.html";
    }

    /**
     * 跳转到修改进货单据查询
     */
    @RequestMapping("/purchaseGoods_update/{purchaseGoodsId}")
    public String purchaseGoodsUpdate(@PathVariable Integer purchaseGoodsId, Model model) {
        PurchaseGoods purchaseGoods = purchaseGoodsService.selectById(purchaseGoodsId);
        model.addAttribute("item",purchaseGoods);
        LogObjectHolder.me().set(purchaseGoods);
        return PREFIX + "purchaseGoods_edit.html";
    }

    /**
     * 跳转到进货单据查询页面
     */
    @RequestMapping("/purchaseGoods_detail/{purchaseId}/{orderNumber}")
    public String purchaseGoodsDetail(@PathVariable(value = "purchaseId") Long purchaseId,
                                      @PathVariable(value = "orderNumber") String orderNumber,
                                      Model model){
        model.addAttribute("purchaseId", purchaseId);
        model.addAttribute("orderNumber", orderNumber);
        return PREFIX + "purchaseGoods_detail.html";
    }


    /**
     * 获取进货单据查询列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return purchaseGoodsService.selectList(null);
    }

    /**
     * 新增进货单据查询
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(PurchaseGoods purchaseGoods) {
        purchaseGoodsService.insert(purchaseGoods);
        return SUCCESS_TIP;
    }

    /**
     * 删除进货单据查询
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer purchaseGoodsId) {
        purchaseGoodsService.deleteById(purchaseGoodsId);
        return SUCCESS_TIP;
    }

    /**
     * 修改进货单据查询
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(PurchaseGoods purchaseGoods) {
        purchaseGoodsService.updateById(purchaseGoods);
        return SUCCESS_TIP;
    }

    /**
     * 进货单据查询详情
     */
    @RequestMapping(value = "/detail/{purchaseId}")
    @ResponseBody
    public Object detail(@PathVariable("purchaseId") Long purchaseId) {
        EntityWrapper<PurchaseGoods> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("purchase_id",purchaseId);
        return purchaseGoodsService.selectList(entityWrapper);
    }
}
